package com.yuexun.dhdoctor.module.contractmanage.adapter;

import android.widget.AbsListView;
import android.widget.LinearLayout;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.contractmanage.bean.HospitalizationRecordBean;
import com.yuexun.dhdoctor.module.contractmanage.bean.OutPatientRecordBean;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * Created by work-jx on 2016/10/28.
 */
public class RecordAdapter extends KJAdapter<Object> {
    //itemA类的type标志
    private static final int TYPE_A = 0;
    //itemB类的type标志
    private static final int TYPE_B = 1;

    public RecordAdapter(AbsListView view, Collection<Object> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder adapterHolder, Object item, boolean isScrolling,final int position) {
        LinearLayout recorddetail_item_main_ll = adapterHolder.getView(R.id.recorddetail_item_main_ll);
        if(position % 2 == 0)
        {
            recorddetail_item_main_ll.setBackgroundColor(mCxt.getResources().getColor(R.color.text_title_color));
        }
        else
        { recorddetail_item_main_ll.setBackgroundColor(mCxt.getResources().getColor(R.color.record_bg));

        }

        int type = getItemViewType(item);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
        SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy");
        SimpleDateFormat formatter_day = new SimpleDateFormat("MM/dd");
        switch (type) {
            case TYPE_A:
                OutPatientRecordBean.RowsBean outPatientRecordBean = (OutPatientRecordBean.RowsBean) item;
                adapterHolder.setText(R.id.recorddetail_item_hospital_name,outPatientRecordBean.getHosp_name());
                adapterHolder.setText(R.id.recorddetail_item_type,"门诊");
                adapterHolder.setText(R.id.recorddetail_item_diagnosis,outPatientRecordBean.getDiag_name());
                adapterHolder.setText(R.id.recorddetail_item_doctor_name,outPatientRecordBean.getCli_doct_name());
                adapterHolder.setText(R.id.recorddetail_item_department,outPatientRecordBean.getCli_dept_name());
                Date date = null;
                try {
                    date = format.parse(outPatientRecordBean.getCli_dtime().substring(0,outPatientRecordBean.getCli_dtime().length()-2));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                adapterHolder.setText(R.id.recorddetail_item_year_tv,formatter_year.format(date));
                adapterHolder.setText(R.id.recorddetail_item_date_tv,formatter_day.format(date));
                break;

            case TYPE_B:
                HospitalizationRecordBean.RowsBean hospitalizationRecordBean = (HospitalizationRecordBean.RowsBean) item;
                adapterHolder.setText(R.id.recorddetail_item_hospital_name,hospitalizationRecordBean.getHosp_name());
                adapterHolder.setText(R.id.recorddetail_item_type,"住院");
                adapterHolder.setText(R.id.recorddetail_item_diagnosis,hospitalizationRecordBean.getInhos_diag_name());
                adapterHolder.setText(R.id.recorddetail_item_doctor_name,hospitalizationRecordBean.getDiag_doct_name());
                adapterHolder.setText(R.id.recorddetail_item_department,hospitalizationRecordBean.getDiag_dept_name());

                Date date_B = null;
                try {
                    date_B = format.parse(hospitalizationRecordBean.getInhos_dtime().substring(0,hospitalizationRecordBean.getInhos_dtime().length()-2));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                adapterHolder.setText(R.id.recorddetail_item_year_tv,formatter_year.format(date_B));
                adapterHolder.setText(R.id.recorddetail_item_date_tv,formatter_day.format(date_B));
                break;
        }

    }


    /**
     * 获得itemView的type
     * @param object
     * @return
     */
    public int getItemViewType(Object object) {
        int result = 0;
        if (object instanceof OutPatientRecordBean.RowsBean) {
            result = TYPE_A;
        }
        else if (object instanceof HospitalizationRecordBean.RowsBean) {
            result = TYPE_B;
        }
        return result;
    }
}
